
#!/usr/bin/python
# -*- coding: utf-8 -*-
import os
import sys
import time
from engine.common import *
if sys.platform == "darwin":
    adbpath = "adb "
else:
    adbpath = "/usr/bin/adb "
if (sys.argv.__len__())<4:
    print( "参数个数少了")
    exit(0)
handset = sys.argv[1]
currentHandsetSnumber = sys.argv[2]
command = sys.argv[3]
areaPara = sys.argv[4]

resultF = sys.argv[5]

setHandsetSno(currentHandsetSnumber)
setHandsetType(handset)
baseDir = "/data/wwwroot/test.dianxi.com/"

def getImageFile( path):
    ret = 'null'
    file_list = os.listdir(path)
    for file in file_list:
        if '.jpg' in file or '.png' in file or '.jpeg' in file:
            ret = file
            break
    debugInfo("getImageFileName ==========="+ret)
    return ret


def calcScreenCenterAndSave(kong ,handset,currentHandsetSnumber,delay=5):

    x,y = getHandsetXY()
    xx = int(int(x)/2)
    yy =int(int(y)/2)
    print(getHandsetFileSavePath("/temp/posx.txt"))
    print(str(xx))
    with open(getHandsetFileSavePath("/temp/posx.txt"),"w") as f:
        f.write(str(xx))
    with open(getHandsetFileSavePath("/temp/posy.txt"), "w") as f:
        f.write(str(yy))


def searchDirForSend(filePath ,resultF,handset,currentHandsetSnumber,delay=5):

    
    if os.path.exists(getHandsetFileSavePath(resultF)+".file"):
        os.system("rm -f "+getHandsetFileSavePath(resultF)+".file")

    retValue = getImageFile(baseDir+"/"+filePath)
    if 'null' in retValue:
        #print 没有了 跳转到4。
        retValue = ''

    print("-------------------------------------"*2)
    print(retValue)
    with open(getHandsetFileSavePath(resultF), 'w')as f:
        if len(retValue) <= 1:
            f.write('True')
            return True  # TRUE 状态转移
        else:
            with open(getHandsetFileSavePath(resultF)+".txt", 'w')as fx:
                fx.write(retValue)

            f.write('False')
            return False




def openImageServer(filePath ,resultF,handset,currentHandsetSnumber,delay=5):

    print("restltF="+resultF)
    with open(getHandsetFileSavePath(resultF))as f:

        fileName =f.read()

    url = "http://test.dianxi.com/"+filePath+"/"+fileName
    print(url)
    os.system(adbpath +" -s "+ currentHandsetSnumber + " shell am start -a android.intent.action.VIEW -d   " + url)
    time.sleep(delay)


def delThisImage(filePath ,resultF,handset,currentHandsetSnumber,delay=5):

    print("restltF="+resultF)


    with open(getHandsetFileSavePath(resultF))as f:

        fileName =f.read()

    print("del file "+" rm "+baseDir+"/"+filePath+"/"+fileName)
    os.system(" rm "+baseDir+"/"+filePath+"/"+fileName)
    time.sleep(delay)

    with open(getHandsetFileSavePath(resultF), 'w')as f:
        if  1:
            f.write('True')
            return True  # TRUE 状态转移






def openTianqi(area ,handset,currentHandsetSnumber,delay=5):
    if "fuzhou" in area:
        url = "https://www.tianqi.com/fuzhou/"
    if "yantai" in area:
        url = "https://www.tianqi.com/yantai/"
    if "shanghai" in area:
        url = "https://www.tianqi.com/shanghai/"
    os.system(adbpath +" -s "+ currentHandsetSnumber + " shell am start -a android.intent.action.VIEW -d   " + url)
    time.sleep(delay)

if command =="openTianqi".strip():
    openTianqi(areaPara,handset,currentHandsetSnumber)
if command =="openImageServer".strip():
    openImageServer(areaPara,resultF,handset,currentHandsetSnumber)
if command == "calcScreenCenterAndSave".strip():
    calcScreenCenterAndSave(areaPara,resultF, handset, currentHandsetSnumber)
if command == "searchDirForSend".strip():
    searchDirForSend(areaPara,resultF, handset, currentHandsetSnumber)

if command == "delThisImage".strip():
    delThisImage(areaPara,resultF, handset, currentHandsetSnumber)
